import "../../utils/formatTime"
import { getStorage, toast, modal } from "../../utils/storage"
Page({
  data: {
    id: '',
    status: { "0": "待审批", "1": "审批不通过", "2": "待发布", "3": "报名中", "4": "进行中", "5": "已结束" },//活动状态(教师)
    studentStatus: { '0': "已报名", "1": "待提交", "2": "待批阅", "3": "已批阅合格", "4": "已批阅不合格" },//参与活动状态(学生)
    testItems: { "1": "签到", "2": "签退", "3": "活动过程照片", "4": "活动总结", "5": "其他" },//考核项
    actTypes: [],
    entity: {},
    showPoint: false,//显示积分分配对话框
    halfYear: '',//所欠积分分配学期
    owedPoints: []
  },
  goback() {
    wx.navigateBack();
  },
  showPointModal() {
    this.setData({ showPoint: true })
  },
  closePointModal() {
    this.setData({ showPoint: false })
  },
  changeYear(e) {
    this.setData({ halfYear: e.currentTarget.dataset.name })
  },
  async submitPointChanged() {
    if (!this.data.halfYear) {
      toast({ title: "请选择要将积分分配的学期" });
      return;
    }
    let { statusCode, data: { code, message = "操作错误，请重试" } } = await wx.$http.post("/quality/student/allocatePointsToHalfYearOfStu", {
      userId: getStorage('userId'),
      actId: this.data.id,
      halfYear: this.data.halfYear
    })
    if (statusCode === 2000 && code === 2000)
      toast({ title: "成功分配学分", icon: "success" })
    else
      toast({ title: message })
  },
  async onLoad({ id }) {
    this.setData({ id })
    let [actTypeRes, actRes, pointRes] = await wx.$http.all(
      wx.$http.post("/quality/actType/getDefaultActTypes"),
      wx.$http.get("/quality/act/activityById", { act_id: id }),
      wx.$http.post("/quality/student/getShortHalfYearPointsOfStu", { userId: getStorage('userId') })
    ).catch();
    if (actTypeRes.statusCode === 200 && actTypeRes.data.code === 200)
      this.setData({ actTypes: actTypeRes.data.data });

    let { statusCode, data: { data: entity, code } } = actRes;
    if (statusCode === 200 && code === 200) {
      entity.actsPoints.forEach(x => {
        let t = this.data.actTypes.find(q => q.actType == x.actPointsType);
        if (t)
          x.actTypeDesc = t.actDesc;
      });
      entity.students.status = Number(entity.students.status);
      entity.actTypeInfo = this.data.actTypes.find(x => x.actType === entity.actType);
      entity.students.evaluationTypes = JSON.parse(entity.students.evaluationTypes).map(x => this.data.testItems[x]);
      if (entity.students.actLimitStuCnts <= 0)
        entity.students.actLimitStuCnts = "不限";
      if (entity.students.actJoinTypeGroupNum <= 0)
        entity.students.actJoinTypeGroupNum = "不限";
      this.setData({ entity });
    }
    if (pointRes.statusCode === 200 && pointRes.data.code === 200)
      this.setData({ owedPoints: pointRes.data.data });
  },
  async signUpActivity() {
    let result = await modal({ content: "确定报名当前活动？" });
    if (!result) return;
    let { statusCode, data: { code, message = "报名失败，请重试" } } = await wx.$http.post("/quality/student/activityApplyForStu", {
      actId: this.data.id
    }).catch();
    if (statusCode === 200 && code === 200) {
      let entity = this.data.entity;
      entity.students.status = 0;
      this.setData({ entity });
      toast({ title: "成功报名活动" });
    }
    else
      toast({ title: message })
  },
  goHomework() {
    wx.navigateTo({
      url: '/pages/student/homework/homework?id=' + this.data.id,
    })
  },
  goHomeworkInfo() {
    wx.navigateTo({
      url: '/pages/student/homeworkinfo/homeworkinfo?id=' + this.data.id,
    })
  },
  onShareAppMessage(){
    return getApp().globalData.shareApp;
  }
})